package com.lyc.attend.controller;

import com.lyc.attend.entity.Attend;
import com.lyc.attend.service.IAttendService;
import com.lyc.common.page.PageQueryBean;
import com.lyc.common.page.PageQueryConditions;
import com.lyc.user.entity.User;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("attend")
public class AttendController {

    @Autowired
    private IAttendService attendService;

    /**
     * 返回打卡统计页面
     * @return 打卡统计页面
     */
    @RequestMapping
    public String attendanceStatisticsView(){
        return "attend";
    }

    /**
     * 打卡接口
     * 1.实际场景应该是APP走到公司内部自动打卡/手动打卡
     * 2.机器人工打卡
     * @param attend 打卡信息
     * @return -1 签到失败 1签到成功
     */
    @RequiresPermissions("attend:signIn")
    @RequestMapping("/signIn")
    @ResponseBody
    public int signIn(@RequestBody Attend attend){
        try {
            attendService.addAttendanceRecord(attend);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 分页条件查询
     * @param queryConditions 分页参数以及查询条件
     * @return 分页结果
     */
    @RequiresRoles(value = {"boss","staff"},logical = Logical.OR)
    @RequestMapping("/pageQuery")
    @ResponseBody
    public PageQueryBean pageQueryByConditions(PageQueryConditions queryConditions, HttpSession session){//form表单key-value方式提交数据 可以直接封装 json数据需要使用@RequestBody注解封装
        User user = (User) session.getAttribute("userInfo");
        String[] rangeDate = queryConditions.getRangeDate().split("/");
        queryConditions.setStartDate(rangeDate[0]);
        queryConditions.setEndDate(rangeDate[1]);
        queryConditions.setUserId(user.getId());
        return attendService.pageQueryByConditions(queryConditions);
    }
}
